<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

require ( '../global/xajax.inc.php' );

function travelTimeAJAX( $army_id, $x_target, $y_target, $x, $y ) {
	$objResponse = new xajaxResponse();
	$objResponse->addAssign( "traveltime", "innerHTML", getTravelTime( $army_id, $x_target, $y_target, $x, $y, true ) );
	return $objResponse;
}
$xajax = new xajax();
//$xajax->debugOn(); // Uncomment this line to turn debugging on
$xajax->registerFunction( "travelTimeAJAX" );
$xajax->processRequests();

aos_header( 'Army' );

if( isset( $_GET[ 'name' ] ) ) {
	$army_name = formatInput( $_GET[ 'name' ] );
}
else {
	$army_name = '';
}
$current_turn = getCurrentTurn();

$query = "SELECT * " .
		"FROM army " .
		"WHERE name = '" . $army_name . "'";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);

if( $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
	$x = $row[ 'x' ];
	$y = $row[ 'y' ];
	$follow = $row[ 'follow' ];
	$actual_x = ( int ) round( $row[ 'actual_x' ] );
	$actual_y = ( int ) round( $row[ 'actual_y' ] );
	$officer_id = $row[ 'officer_id' ];
	$instructions = $row[ 'instructions' ];
	$army_id = $row[ "army_id" ];
	$scout_info = 0;
	$infantry_info = 0;
	$cavalry_info = 0;
	$artillery_info = 0;
	$scout_info_min = 0;
	$infantry_info_min = 0;
	$cavalry_info_min = 0;
	$artillery_info_min = 0;
	$ready = true;
	$arrived_turn = $row[ 'arrived_turn' ];
	$action = "";
	$fatigue = $row[ 'fatigue' ];
	$max_fatigue = $row[ 'max_fatigue' ];
	
	$query = "SELECT * " .
			"FROM army " .
			"WHERE army_id = " . $army_id;
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	
	$size = $row[ 'scout' ] + $row[ 'infantry' ] + $row[ 'cavalry' ] + $row[ 'artillery' ];
	
	$fortified = false;
	
	$query2 = "SELECT * " .
			"FROM transfer " .
			"WHERE ( from_army_id = $army_id OR to_army_id = $army_id ) " .
			"ORDER BY complete_turn ASC";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$num2 = mysql_numrows( $result2 );
	$i2 = 0;
	while ( $i2 < $num2 ) {
		if( mysql_result( $result2, $i2, "from_army_id" ) == $army_id ) { // units are beeing removed from this army
			$action = mysql_result( $result2, $i2, 'complete_turn' ) - $current_turn;
			if( mysql_result( $result2, $i2, 'scouts' ) > 0 ) {
				$scout_info_min += mysql_result( $result2, $i2, 'scouts' );
			}
			if( mysql_result( $result2, $i2, 'infantry' ) > 0 ) {
				$infantry_info_min += mysql_result( $result2, $i2, 'infantry' );
			}
			if( mysql_result( $result2, $i2, 'cavalry' ) > 0 ) {
				$cavalry_info_min += mysql_result( $result2, $i2, 'cavalry' );
			}
			if( mysql_result( $result2, $i2, 'artillery' ) > 0 ) {
				$artillery_info_min += mysql_result( $result2, $i2, 'artillery' );
			}
		}
		else {
			$action = mysql_result( $result2, $i2, 'complete_turn' ) - $current_turn;
			if( mysql_result( $result2, $i2, 'scouts' ) > 0 ) {
				$scout_info += mysql_result( $result2, $i2, 'scouts' ) . ")";
			}
			if( mysql_result( $result2, $i2, 'infantry' ) > 0 ) {
				$infantry_info += mysql_result( $result2, $i2, 'infantry' ) . ")";
			}
			if( mysql_result( $result2, $i2, 'cavalry' ) > 0 ) {
				$cavalry_info += mysql_result( $result2, $i2, 'cavalry' ) . ")";
			}
			if( mysql_result( $result2, $i2, 'artillery' ) > 0 ) {
				$artillery_info += mysql_result( $result2, $i2, 'artillery' ) . ")";
			}
		}
		$ready = false;
		$i2 ++;
	}
	
	if( !$ready ) {
		$action = translate( "Soldiers are being transfered, the army will be available in" ) . " " . number_format( $action ) . " " . translate( "days" );
		$scout_info = formatAddLeave( $scout_info, $scout_info_min );
		$infantry_info = formatAddLeave( $infantry_info, $infantry_info_min );
		$cavalry_info = formatAddLeave( $cavalry_info, $cavalry_info_min );
		$artillery_info = formatAddLeave( $artillery_info, $artillery_info_min );
	}
	else {
		$scout_info = "";
		$infantry_info = "";
		$cavalry_info = "";
		$artillery_info = "";
	}
	
	if( $ready && $row[ 'mission' ] > 0 ) {
		$action = translate( "On a mission" ) . ", " . $row[ 'mission' ] . " " . translate( "days remaining" ) . ".";
		$ready = false;
	}
	elseif( $ready ) {
		$query2 = "SELECT count(*) FROM army_orders WHERE army_id = " . $army_id;
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		if( $row2[ 'count(*)' ] > 0 ) {
			$query2 = "SELECT * FROM army_orders WHERE army_id = " . $army_id . " AND rank = 0";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			
			if( $row2[ 'order_type' ] == 1 && $row2[ 'complete_turn' ] > 0 ) {
				$action = translate( 'Moving to' ) . " <a href=\"../operations/map.php?x=" . ( $row2[ 'order_value' ] - 20 ) . "&y=" . ( $row2[ 'order_value2' ] - 20 ) . "\">" .
						"(" . $row2[ 'order_value' ] . ", " . $row2[ 'order_value2' ] . ")</a>, ";
				$action .= ( $row2[ 'complete_turn' ] - $current_turn ) . " " . translate( "days remaining" ) . ".";
			}
			else {
				$action = translate( "Following given orders" );
			}
			$ready = false;
		}
	}

	if( $ready && $follow > -1 ) {
		$action = translate( "Following" );
		$query5 = "SELECT name " .
				"FROM army " .
				"WHERE army_id = " . $follow;
		$result5 = mysql_query( $query5 ) or die( "Query failed : " . mysql_error() );
		$row5 = mysql_fetch_array( $result5, MYSQL_ASSOC);
		$action .= ' ' . $row5[ 'name' ];
		$ready = false;
		$ready_but_following = true;
	}
	else {
		$ready_but_following = false;
	}

	if( $ready ) {
		if( $row[ 'fortified' ] == 1 ) {
			$action = translate( "Fortified." );
			$fortified = true;
		}
		elseif( $row[ 'travel' ] > 0 ) {
			$action = translate( "Finishing movement." );
			$ready = false;
		}
		else {
			$action = translate( "Waiting for orders." );
		}
	}
	
	printGlobalSoldiers( $_SESSION[ 'ruler_id' ] );
	include ( "../global/resources.php" );
?>
						<br />
<?php
	if( isset( $_SESSION[ 'add_army_status' ] ) ) {
?>
						<div class="blok">
							<strong><?php transprint( 'Status' ); ?></strong>: <?php print( $_SESSION[ 'add_army_status' ] ); ?>
						</div>
						<br />
<?php
		unset( $_SESSION[ 'add_army_status' ] );
	}
	$query = "SELECT name, army_id " .
			"FROM army " .
			"WHERE ruler_id = " . $_SESSION['ruler_id'] . " " .
			"ORDER BY name";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	$i = 0;
	while ( $i < $num ) {
		if( mysql_result( $result, $i, "army_id" ) == $army_id ) {
			if( $i < $num - 1 ) {
				print( "<div id=\"text_right\"><a href=\"army.php?name=" . mysql_result( $result, ( $i + 1 ), "name" ) . "\">" . translate( "Next" ) . "</a></div>\r\n" );
			}
			if( $i > 0 ) {
				print( "<div id=\"text_left\"><a href=\"army.php?name=" . mysql_result( $result, ( $i - 1 ), "name" ) . "\">" . translate( "Previous" ) . "</a></div>\r\n" );
			}
			print( "<div id=\"text_center\"><h2>" . addslashes( $army_name ) . "</h2></div>\r\n" );			
		}
		$i ++;
	}

	$from_scout = getSessionValue( 'transfer_from_scouts' );
	$from_infantry = getSessionValue( 'transfer_from_infantry' );
	$from_cavalry = getSessionValue( 'transfer_from_cavalry' );
	$from_artillery = getSessionValue( 'transfer_from_artillery' );
	$to_scout = getSessionValue( 'transfer_to_scouts' );
	$to_infantry = getSessionValue( 'transfer_to_infantry' );
	$to_cavalry = getSessionValue( 'transfer_to_cavalry' );
	$to_artillery = getSessionValue( 'transfer_to_artillery' );
?>
						<div class="blok">
<?php	
	if( $ready ) {
		$query = "SELECT count(*) FROM upgrade_status WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND achieved = 1 AND upgrade_id = 7";
		$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		if( $row2[ 'count(*)' ] > 0 ) {
			$cavalry = true;
		}
		else {
			$cavalry = false;
		}

		$query = "SELECT count(*) FROM upgrade_status WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND achieved = 1 AND upgrade_id = 9";
		$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		if( $row2[ 'count(*)' ] > 0 ) {
			$artillery = true;
		}
		else {
			$artillery = false;
		}
		
		print( "<div id=\"floatright\">" );

		$query = "SELECT ruler_id " .
				"FROM village " .
				"JOIN village_buildings ON village.village_id = village_buildings.village_id " .
				"WHERE x = " . $x . " AND y = " . $y . " AND building_id = 22";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( isset( $row [ 'ruler_id' ] ) && $row [ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] && !$fortified ) {
			if( isset( $_SESSION[ 'train_scouts' ] ) ) {
				$train_scouts = $_SESSION[ 'train_scouts' ];
				unset( $_SESSION[ 'train_scouts' ] );
			}
			else {
				$train_scouts = 0;
			}

			if( isset( $_SESSION[ 'train_infantry' ] ) ) {
				$train_infantry = $_SESSION[ 'train_infantry' ];
				unset( $_SESSION[ 'train_infantry' ] );
			}
			else {
				$train_infantry = 0;
			}
			
			if( isset( $_SESSION[ 'train_cavalry' ] ) ) {
				$train_cavalry = $_SESSION[ 'train_cavalry' ];
				unset( $_SESSION[ 'train_cavalry' ] );
			}
			else {
				$train_cavalry = 0;
			}
			
			if( isset( $_SESSION[ 'train_artillery' ] ) ) {
				$train_artillery = $_SESSION[ 'train_artillery' ];
				unset( $_SESSION[ 'train_artillery' ] );
			}
			else {
				$train_artillery = 0;
			}
?>			
					<br />
					<form action="add_to_army.php" method="post">
						<table id="training">
							<tr>
								<th colspan="5"><?php transprint( 'Train recruits into soldiers' ); ?></th>
							</tr>
							<tr>
								<td><?php transprint( 'Unit type' ); ?></td>
								<td><?php transprint( 'Gold cost' ); ?></td>
								<td><?php transprint( 'Iron cost' ); ?></td>
								<td><?php transprint( 'Coal cost' ); ?></td>
								<td><?php transprint( 'Amount' ); ?></td>
							</tr>
							<tr>
								<td><?php transprint( 'Scout' ); ?></td>
								<td align="right"><strong class="gold">3</strong></td>
								<td align="right"><strong class="iron">0</strong></td>
								<td align="right"><strong class="coal">0</strong></td>
								<td><input type="text" size="5" value="<?php print( $train_scouts ); ?>" name="scout" /></td>
							</tr>
							<tr>
								<td><?php transprint( 'Infantry' ); ?></td>
								<td align="right"><strong class="gold">1</strong></td>
								<td align="right"><strong class="iron">6</strong></td>
								<td align="right"><strong class="coal">9</strong></td>
								<td><input type="text" size="5" value="<?php print( $train_infantry ); ?>" name="infantry" /></td>
							</tr>
<?php
			if( $cavalry ) {
?>
							<tr>
								<td><?php transprint( 'Cavalry' ); ?></td>
								<td align="right"><strong class="gold">10</strong></td>
								<td align="right"><strong class="iron">36</strong></td>
								<td align="right"><strong class="coal">24</strong></td>
								<td><input type="text" size="5" value="<?php print( $train_cavalry ); ?>" name="cavalry" /></td>
							</tr>
<?php
			}
			if( $artillery ) {
?>
							<tr>
								<td><?php transprint( 'Artillery' ); ?></td>
								<td align="right"><strong class="gold">6</strong></td>
								<td align="right"><strong class="iron">48</strong></td>
								<td align="right"><strong class="coal">96</strong></td>
								<td><input type="text" size="5" value="<?php print( $train_artillery ); ?>" name="artillery" /></td>
							</tr>
<?php		
			}
?>
						</table>
						<input type="submit" name="calc" value="<?php transprint( 'Calculate resources' ); ?>" /> <input type="submit" name="train" value="<?php transprint( 'Train' ); ?>" />
						<input type="hidden" name="army_id" value="<?php print( $army_id ); ?>" />
					</form><br />
<?php
		}
		
		$allow_transfer = allow_transfer( $x, $y );
		
		if( $allow_transfer ) {
?>			
					<br />
					<form action="start_transfer.php" method="post">
						<table id="transfer">
							<tr>
								<th colspan="3"><?php transprint( 'Transfer soldiers' ); ?></th>
							</tr>
							<tr>
								<td><?php transprint( 'Type' ); ?></td>
								<td><?php transprint( 'From army' ); ?></td>
								<td><?php transprint( 'To army' ); ?></td>
							</tr>
							<tr>
								<td><?php transprint( 'Scouts' ); ?></td>
								<td><input type="text" size="5" name="from_scouts" value="<?php print( $from_scout ); ?>" /></td>
								<td><input type="text" size="5" name="to_scouts" value="<?php print( $to_scout ); ?>" /></td>
							</tr>
							<tr>
								<td><?php transprint( 'Infantry' ); ?></td>
								<td><input type="text" size="5" name="from_infantry" value="<?php print( $from_infantry ); ?>" /></td>
								<td><input type="text" size="5" name="to_infantry" value="<?php print( $to_infantry ); ?>" /></td>
							</tr>
<?php
			if( $cavalry ) {
?>
							<tr>
								<td><?php transprint( 'Cavalry' ); ?></td>
								<td><input type="text" size="5" name="from_cavalry" value="<?php print( $from_cavalry ); ?>" /></td>
								<td><input type="text" size="5" name="to_cavalry" value="<?php print( $to_cavalry ); ?>" /></td>
							</tr>
<?php
			}
			if( $artillery ) {
?>
							<tr>
								<td><?php transprint( 'Artillery' ); ?></td>
								<td><input type="text" size="5" name="from_artillery" value="<?php print( $from_artillery ); ?>" /></td>
								<td><input type="text" size="5" name="to_artillery" value="<?php print( $to_artillery ); ?>" /></td>
							</tr>
<?php
			}
?>
						</table>
						<input type="checkbox" value="add_all" name="add_all" /> <?php transprint( 'Add all trained reserves to the army' ); ?><br />
						<input type="checkbox" value="remove_all" name="remove_all" /> <?php transprint( 'Remove all soldiers from the army' ); ?><br />
						<input type="submit" value="<?php transprint( 'Transfer' ); ?>" />
						<input type="hidden" name="army_id" value="<?php print( $army_id ); ?>" />
					</form>
<?php
		}
		print( "</div>\r\n" );
	}
?>
				<div class="army_block">
					<strong><?php transprint( "Information" ); ?></strong><br />
<?php	
	$query = "SELECT * FROM " .
			"village " .
			"WHERE x = " . $actual_x . " AND y = " . $actual_y;
	$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	
	$location = "";
	if( isset( $row2[ 'village_id' ] ) ) {
		$loyalty = $row2[ 'loyalty' ];
		$loyal_to = $row2[ 'loyal_to' ];
		$population = $row2[ 'population' ];
		$village_ruler_id = $row2[ 'ruler_id' ];
		$village_status = getVillageStatus( $row2[ 'village_id' ] );
		$color = getStatusColor( $village_status );
		if( $village_status == 3 ) {
			$location = "<strong id=\"$color\">" . translate( "independent village" ) . "</strong> ";
		}
		else if( $row2[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
			$location =  "<a href=\"../villages/village.php?name=" . $row2[ 'name' ] . "\"><strong id=\"$color\">" . $row2[ 'name' ] . "</strong></a> ";
		}
		else {
			$location =  "<strong id=\"$color\">" . $row2[ 'name' ] . "</strong> ";
		}
	}
	$location .= "<a href=\"../operations/map.php?x=" . round( $actual_x - 20 ) . "&y=" . round( $actual_y - 20 ) . "\">(" . $actual_x . "," . $actual_y . ")</a><br/>\r\n";
	
	$query = "SELECT * FROM army WHERE army_id = '" . $army_id . "'";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	
	if( $x != $actual_x || $y != $actual_y ) {
		print( translate( "Moving through or close to" ) . ": $location" );
	}
	else {
		print( translate( "Current location" ) . ": $location" );
		if( isset( $row2[ 'village_id' ] ) && $row2[ 'ruler_id' ] != -1 && ( $village_ruler_id != $_SESSION[ 'ruler_id' ] || $loyal_to != $_SESSION[ 'ruler_id' ] ) ) {
			$resistance = $population * $loyalty * 0.01;
			$defense = $row[ 'scout' ] * 4 + $row[ 'infantry' ] * 6 + $row[ 'cavalry' ] * 30 + $row[ 'artillery' ] * 48;
			if( $defense > $resistance ) {
				transprint( 'This army can deal with the population of this village.' );
			}
			else {
				transprint( 'This army can\'t deal with the population of this village.' );
			}
			print( '<br />' );
		}
	}
	
	if( $fatigue < 5 ) {
		$fatigue_text = '<strong class="red">' . $fatigue . '</strong>';
	}
	else {
		$fatigue_text = $fatigue;
	}
?>
			<?php transprint( 'Supply' ); ?>: <?php print( $fatigue_text ); ?> <?php transprint( 'out of' ); ?> <?php print( $max_fatigue )?><br />
<?php
	$query2 = "SELECT * FROM officers WHERE officer_id = " . $officer_id;
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );

	if( isset( $row2[ 'name' ] ) ) {
		printOfficerRankIcon( $row2[ 'rank' ] );
		print( "<a class=\"officer-in-army-page\" href=\"officer.php?name=" .  $row2[ 'name' ] . "\">" . $row2[ 'name' ] . "</a>" );
		print( translate( "Commanding officer" ) . ': ' );
	}
	else {
		print( translate( "Commanding officer" ) . ": " . translate( "None" ) );
	}
	print( "<br />\r\n" );

	if( isset( $row2[ 'rank' ] ) ) {
		$maxsize = 50;
		for( $i = 0; $i < $row2[ 'rank' ]; $i ++ ) {
			$maxsize *= 5;
		}
	}
	else {
		$maxsize = 10;
	}
	
	$strong = '';
	$end = '';
	if( $size > $maxsize ) {
		$strong = '<strong class="red">';
		$end = '</strong>';
	}
	print( $strong . translate( "Army size" ) . ": " . number_format( $size ) . "$end<br />\r\n" ); 
	print( $strong . translate( "Maximum size" ) . ": " . number_format( $maxsize ). "$end<br />\r\n" );
	print( translate( "Status" ) . ": " . $action . "<br />\r\n" );
	
	if( ( $row[ 'scout' ] + $row[ 'infantry' ] + $row[ 'cavalry' ] + $row[ 'artillery' ] ) > 0 ) {
		print( "<br /><strong>" . translate( "Composition" ) . "</strong><br />\r\n" );
	}
	
	if( $row[ 'scout' ] > 0 || $scout_info != "" ) {
		print( translate( "Scouts" ) . ": <strong class=\"scouts\">" . number_format( $row[ 'scout' ] ) . "</strong>$scout_info<br/>\r\n" );
	}
	else {
		print( "<br />\r\n" );
	}
	
	if( $row[ 'infantry' ] > 0 || $infantry_info != "" ) {
		print( translate( "Infantry" ) . ": <strong class=\"infantry\">" . number_format( $row[ 'infantry' ] ) . "</strong>$infantry_info<br/>\r\n" );
	}
	else {
		print( "<br />\r\n" );
	}

	if( $row[ 'cavalry' ] > 0 || $cavalry_info != "" ) {
		print( translate( "Cavalry" ) . ": <strong class=\"cavalry\">" . number_format( $row[ 'cavalry' ] ) . "</strong>$cavalry_info<br/>\r\n" );
	}
	else {
		print( "<br />\r\n" );
	}

	if( $row[ 'artillery' ] > 0 || $artillery_info != "" ) {
		print( translate( "Artillery" ) . ": <strong class=\"artillery\">" . number_format( $row[ 'artillery' ] ) . "</strong>$artillery_info<br/>\r\n" );
	}
	else {
		print( "<br />\r\n" );
	}

	if( $ready && $size == 0 ) {
?>
						<form action="remove_army.php" method="post">
							<input type="hidden" value="<?php print( $army_id ); ?>" name="army_id" />
							<input type="submit" value="<?php transprint( 'delete army' ); ?>" />
						</form><br />
<?php
	}

	if( $ready ) {
?>
						<br />
						<form action="../operations/start_mission.php" method="post">
							<input type="hidden" value="true" name="<?php print( $army_id ); ?>" />
							<input type="submit" value="<?php transprint( 'Send on a mission' ); ?>" />
						</form><br />
<?php
	}
	else {
		print( "<br /><br />" );
	}
	
	if( $officer_id > -1 ) {
		if( $follow > -1 ) {
			$query2 = "SELECT instructions " .
					"FROM army " .
					"WHERE army_id = $follow";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC);
			if( $row2[ 'instructions' ] == 0 ) {
				$instruction_text = 'Stand ground';
			}
			elseif( $row2[ 'instructions' ] == 1 ) {
				$instruction_text = 'Defensive';
			}
			elseif( $row2[ 'instructions' ] == 2 ) {
				$instruction_text = 'Aggressive';
			}
			$instruction_text = translate( $instruction_text );
?>
						<br />
						<a href="../aosmanual/concepts.php#Army instructions"><?php transprint( 'Following an army with instructions' ); ?></a>: <?php print( $instruction_text ); ?><br />
						<br />
<?php
		}
		else {
			$checked = Array( '0' => '', '1' => '', '2' => '' );
			$checked[$instructions] = ' selected="selected"';
?>
						<br />
						<form method='post' action='change_instructions.php'>
							<a href="../aosmanual/concepts.php#Army instructions"><?php transprint( 'Army instructions' ); ?></a>: <select name="instructions">
								<option value="0"<?php print( $checked[0] ); ?>><?php transprint( 'Stand ground' ); ?></option>
								<option value="1"<?php print( $checked[1] ); ?>><?php transprint( 'Defensive' ); ?></option>
								<option value="2"<?php print( $checked[2] ); ?>><?php transprint( 'Aggressive' ); ?></option>
							</select><br />
							<input type="hidden" name="army_id" value="<?php print( $army_id ); ?>" />
							<input type="submit" value="<?php transprint( 'Confirm' ); ?>" />
						</form>
						<br />
<?php		
		}
	}
?>	
						<form id="traveltime_form" method="post" action="javascript:getTravelTime( this )">
							<br />
							<strong><?php transprint( 'Calculate travel time' ); ?></strong><br />
							<?php transprint( 'From' ); ?>:<br />
							<input type="text" size="1" id="x" value="<?php print( $x ); ?>" />, <input type="text" size="1" id="y" value="<?php print( $y ); ?>" /><br />
							<?php transprint( 'To' ); ?>:<br />
							<input type="text" size="1" id="x_target" value="<?php print( $x ); ?>" />,	<input type="text" size="1" id="y_target" value="<?php print( $y ); ?>" />
							<input type="hidden" id="army_id" value="<?php print( $army_id ); ?>" />
							<input type="submit" value="<?php transprint( 'calculate' ); ?>" /><br />
							<?php transprint( 'Result' ); ?>: 
							<div id="traveltime">&nbsp;</div>
						</form>
					</div> <!-- close army_block -->
				</div> <!-- close blok -->
<?php	
	if( $ready && $row[ 'scout' ] == 0 && $row[ 'infantry' ] == 0 && $row[ 'artillery' ] == 0 && $row[ 'cavalry' ] == 0 ) {
		$ready = false;
	}

	if( isset( $_SESSION[ 'conquer_village_status' ] ) ) {
		print( "<br /><div class=\"blok\">Status: " . $_SESSION[ 'conquer_village_status' ] . "</div>\r\n" );
		unset( $_SESSION[ 'conquer_village_status' ] );
	}
	
	if( isset( $_SESSION[ 'buy_village_status' ] ) ) {
		print( "<br /><div class=\"blok\">Status: " . $_SESSION[ 'buy_village_status' ] . "</div>\r\n" );
		unset( $_SESSION[ 'buy_village_status' ] );
	}

	$query = "SELECT count(*) " .
			"FROM village " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$total_villages = $row[ 'count(*)' ];

	$query = "SELECT * " .
			"FROM village " .
			"WHERE x = " . $x . " AND y = " . $y;
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	

	if( $ready && isset( $row[ 'ruler_id' ] ) && $row[ 'ruler_id' ] != $_SESSION[ 'ruler_id' ]  ) { 
		// not own village
		$more = false;
		$village_id = $row[ 'village_id' ];
		$village_name = $row[ 'name' ];
		$village_second_name = $row[ 'second_name' ];
		if( !isset( $village_name ) ) {
			$village_name = "";
		}
		$query3 = "SELECT faction_id " .
				"FROM faction_member " .
				"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND requested = 0 AND invited = 0";
		$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
		if( isset( $row3[ 'faction_id' ] ) ) {
			$query4 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . $row[ 'ruler_id' ] . " AND requested = 0 AND invited = 0";
			$result4 = mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
			$row4 = mysql_fetch_array( $result4, MYSQL_ASSOC );
			if( isset( $row4[ 'faction_id' ] ) && $row4[ 'faction_id' ] == $row3[ 'faction_id' ] ) {
				$more = false;
			}
			else {
				$more = true;
			}
		}
		else {
			$more = true;
		}
		
		if( $more ) {
			if( checkRangeVillage( $village_id ) ) {
				$blocked = false;
				$query2 = "SELECT * FROM army WHERE x = " . $x . " AND y = " . $y . " AND travel = 0 AND scout + infantry + cavalry + artillery > 0";
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$num = mysql_numrows( $result2 );
				$i = 0;
				while ( $i < $num ) {
					if( $_SESSION[ 'ruler_id' ] != mysql_result( $result2, $i, "ruler_id" ) ) {
						$query3 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND requested = 0 AND invited = 0";
						$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
						$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
						if( isset( $row3[ 'faction_id' ] ) ) {
							$tmp_faction_id = $row3[ 'faction_id' ];
							$query3 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . mysql_result( $result2, $i, "ruler_id" ) . " AND requested = 0 AND invited = 0";
							$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
							$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
							if( isset( $row3[ 'faction_id' ] ) && $row3[ 'faction_id' ] != $tmp_faction_id ) {
								$blocked = true;
							}
						}
						else {
							$blocked = true;
						}
					}
					$i ++;
				}
				
				$here_for = -1;
				if( $arrived_turn != -1 ) {
					$here_for = $current_turn - $arrived_turn;
				}
				
				$query3 = "SELECT turns_inactive " .
						"FROM game";
				$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
				$turns_to_inactive = $row3[ 'turns_inactive' ];
					
				if( !$blocked ) {
					if( ( $total_villages < getVillageLimit() && $row[ 'start_place' ] != 1 ) || ( $row[ 'start_place' ] == 1 && $row[ 'ruler_id' ] != -1 ) ) {
						$capital = ( $row[ 'start_place' ] == 1 );
						if( $capital ) {
							$query5 = "SELECT resigned, gold, iron, coal, active_turn " .
									"FROM ruler " .
									"WHERE ruler_id = " . $row[ 'ruler_id' ];
							$result5 = mysql_query( $query5 ) or die( "Query failed : " . mysql_error() );
							$row5 = mysql_fetch_array( $result5, MYSQL_ASSOC );
							if( ( $row5[ 'resigned' ] == 1 || $row5[ 'active_turn' ] + $turns_to_inactive < $current_turn ) && $row5[ 'gold' ] < 500 && $row5[ 'iron' ] < 1000 && $row5[ 'coal' ] < 1500 ) {
								$query5 = "SELECT count(*) " .
										"FROM village " .
										"WHERE ruler_id = " . $row[ 'ruler_id' ];
								$result5 = mysql_query( $query5 ) or die( "Query failed : " . mysql_error() );
								$row5 = mysql_fetch_array( $result5, MYSQL_ASSOC );
								if( $row5[ 'count(*)' ] == 1 ) {
									$capital = false;
								}
							}
						}
						
						
						if( ( $capital && $here_for >= 12 ) || ( $row[ 'ruler_id' ] == -1 && $here_for >= 4 && !$capital ) || ( $here_for >= 0 && !$capital && $row[ 'ruler_id' ] != -1 ) ) {
							print( "<br /><div class=\"blok\">" );
							if( $capital && $here_for >= 12 ) {
								print( "<strong>" . translate( "Loot capital" ) . "</strong><br />\r\n" );
							}
							elseif( !$capital && ( ( $row[ 'ruler_id' ] == -1 && $here_for >= 4 ) || $row[ 'ruler_id' ] != -1 ) ) {
								print( "<strong>" . translate( "Take control of this village" ) . "</strong><br />\r\n" );
							}
							
							if( $row[ 'ruler_id' ] == -1 && $village_name == "" && $village_second_name == '' ) {
								$suggested_name = "";
								$query3 = "SELECT name FROM village_names WHERE used = 0 ORDER BY RAND() LIMIT 1";
								$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
								$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
								if( isset( $row3[ 'name' ] ) ) {
									$suggested_name = $row3[ 'name' ];
								}
							}
							elseif( $village_second_name != '' ) {
								$suggested_name = $village_second_name;
							}
							else {
								$suggested_name = $village_name;
							}
							
							if( $officer_id > 0 && $row[ 'ruler_id' ] == -1 && !$capital && $here_for >= 4 ) {
								$gold_cost = get_bribe_cost( $row[ 'population' ], $officer_id );
								print( "<form action=\"buy_village.php\" method=\"post\">" );
								print( "<input type=\"hidden\" name=\"village_id\" value=\"" . $row[ 'village_id' ] .  "\"/>\r\n" );
								print( translate( "Bribe village officials to join you for" ) . " <strong class=\"gold\">" . number_format( $gold_cost ) . "</strong> " . translate( "gold" ) . ". (" . translate( "enter village name" ) . ")<br />\r\n" );
								print( "<input type=\"text\" value=\"$suggested_name\" name=\"village_name\" />" );
								//print( "<input type=\"hidden\" value=\"$gold_cost\" name=\"gold_cost\" />" );
								print( " <input type=\"submit\" value=\"" . translate( "Bribe" ) . "\" /><input type=\"hidden\" name=\"army_id\" value=\"$army_id\" />" );
								print( "</form><br /><br />\r\n" );
							}
							
							if( ( $here_for >= 12 && $capital ) || ( $here_for >= 0 && !$capital ) ) {
								if( $village_second_name != '' ) {
									$village_name = $village_second_name;
								}
								elseif( !isset( $village_name ) || $village_name == '' ) {
									$village_name = $suggested_name;
								}
								print "<form action=\"conquer_village.php\" method=\"post\">";
								print "<input type=\"hidden\" name=\"village_id\" value=\"" . $row[ 'village_id' ] .  "\"/>\r\n";
								if( !$capital && ( ( $row[ 'ruler_id' ] == -1 && $here_for >= 4 ) || $row[ 'ruler_id' ] != -1 ) ) {
									print( translate( "By force <small>(you can change the village name)</small>" ) . "<br />\r\n" );
									print( "<input type=\"text\" value=\"$village_name\" name=\"village_name\" />" );
									print( " <input type=\"submit\" value=\"" . translate( "Attempt conquest" ) . "\" /><input type=\"hidden\" name=\"army_id\" value=\"$army_id\" />" );
								}
								else {
									$query3 = "SELECT gold, coal, iron, active_turn, resigned " .
											"FROM ruler " .
											"WHERE ruler_id = " . $row[ 'ruler_id' ];
									$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
									$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
									$modifier = 30000;
									if( $row3[ 'resigned' ] == 1 || $row3[ 'active_turn' ] + $turns_to_inactive < $current_turn ) {
										$modifier = 0;
									}
									$gold = $row3[ 'gold' ];
									if( $gold < $modifier ) {
										$gold = 0;
									}
									$iron = $row3[ 'iron' ];
									if( $iron < $modifier * 2.5 ) {
										$iron = 0;
									}
									$coal = $row3[ 'coal' ];
									if( $coal < $modifier * 5 ) {
										$coal = 0;
									}
									
									$resources = $gold * 5 + $iron * 2 + $coal;
									
									if( $resources 		> 5000000 ) {
										$rating = "The capital looks extremely wealthy.";
									}
									elseif( $resources  > 1000000 ) {
										$rating = "The capital looks very wealthy.";
									}
									elseif( $resources  >  500000 ) {
										$rating = "The capital looks wealthy.";
									}
									elseif( $resources  >  100000 ) {
										$rating = "The capital doesn't look particularly wealthy.";
									}
									elseif( $resources  >   50000 ) {
										$rating = "The capital looks a bit poor.";
									}
									elseif( $resources >    10000 ) {
										$rating = "The capital looks poor.";
									}
									elseif( $resources >     4000 ) {
										$rating = "The capital looks very poor.";
									}
									elseif( $resources ==       0 ) {
										$rating = "There is nothing to loot in this capital.";
									}
									else {
										$rating = "There is almost nothing to loot in this capital.";
									}
									$rating = translate( $rating );
									print( $rating . "<br />" );
									print( "<input type=\"submit\" value=\"" . translate( "Loot" ) . "\" /><input type=\"hidden\" name=\"army_id\" value=\"$army_id\" />" );
								}
								print "</form>";
							}
							print "</div>";
						}
						else {
?>
						<br />
						<div class="blok">
<?php
							if( $capital ) {
								transprint( 'You can only loot a capital after staying at the capital for 12 days.' );
								print( '<br />' );
								print( translate( 'There are' ) . ' ' . ( 12 - $here_for ) . ' ' . translate( 'days remaining' ) . '.<br />' );
							}
							elseif( $row[ 'ruler_id' ] == -1 ) {
								print( translate( 'You can bribe a village after staying at the village for 4 days.' ) . '<br />' );
								print( translate( 'There are' ) . ' ' . ( 4 - $here_for ) . ' ' . translate( 'days remaining' ) . '.<br />' );
							}
							else {
								print( translate( 'You can conquer a village after staying at the village for 0 days.' ) . '<br />' );
								print( translate( 'There are' ) . ' ' . ( 0 - $here_for ) . ' ' . translate( 'days remaining' ) . '.<br />' );
							}
?>
						</div>
<?php
						}
					}
					else {
						print( "<br/><div class=\"blok\">" );
						if( $row[ 'start_place' ] == 1 ) {
							print( translate( "You can't loot capitals which are not occupied yet." ) . "<br/>\r\n" );
						}
						else {
							print( translate( "You already control a maximum amount of villages" ) . " (" . getVillageLimit() . "), " . translate( "upgrade your infrastructure to be able to control more." ) . "<br/>\r\n" );
						}
						print( "</div>" );
					}
				}
				else {
					print "<br/><div class=\"blok\">";
					print( translate( "There is a battle going on currently, you can not conquer villages during battle!" ) . "<br/>\r\n" );
					print "</div>";					
				}
			}
			else {
				print( "<br/><div class=\"blok\">" );
				print( translate( "You need to improve your comminication to be able to attack a village this far from your capital." ) );
				print( "</div>" );
			}
		}
	}
	print( "<br />\r\n" );
	
	if( isset( $_SESSION[ 'que_order_status' ] ) ) {
		print "<div class=\"blok\">" . translate( 'Queue order status' ) . ": " . $_SESSION[ 'que_order_status' ] . "</div><br />\r\n";
		unset( $_SESSION[ 'que_order_status' ] );
	}
	
	$query = "SELECT * FROM army_orders " .
			"WHERE army_id = " . $army_id . " ORDER BY rank";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	
	$i = 0;
	if( $num > 0 ) {
		$calc_turn = $current_turn;
		
		$query2 = "SELECT x, y FROM army WHERE army_id = $army_id";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		
		$current_x = $row2[ 'x' ];
		$current_y = $row2[ 'y' ];
		
		print "<table id=\"twothird\"><th colspan=\"5\">" . translate( "Orders" ) . "</th>\r\n";
		print "<tr><td>" . translate( "Rank" ) . "</td><td>" . translate( "Order type" ) . "</td><td>" . translate( "Description" ) . "</td><td>" . translate( "ETA" ) . "</td><td>" . translate( "Delete" ) . "</td></tr>\r\n";
		while ( $i < $num ) {
		    if( $i % 2 == 0 ) {
		        print "<tr id=\"even\">";
		    }
		    else {
		        print "<tr>";
		    }
			
			if( mysql_result( $result, $i, "rank" ) > 0 ) {
				print( "<td align=\"right\">" . mysql_result( $result, $i, "rank" ) . "</td>\r\n" );
			}
			else {
				print( "<td>&nbsp;</td>" );
			}
			
			if( mysql_result( $result, $i, "order_type" ) == 1 ) {
				$order_type = translate( "Move to" );
				if( $calc_turn > 0 ) {
					if( mysql_result( $result, $i, "complete_turn" ) == 0 ) {
						$travel_time = getTravelTime( $army_id, mysql_result( $result, $i, "order_value" ), mysql_result( $result, $i, "order_value2" ), $current_x, $current_y );
						if( $travel_time > 0 ) {
							$calc_turn += $travel_time + 1;
						}
						else {
							$travel_time = '';
							$calc_turn = -1;
						}
					}
					else {
						$calc_turn += mysql_result( $result, $i, "complete_turn" ) - $current_turn;
					}
				}
				$current_x = mysql_result( $result, $i, "order_value" );
				$current_y = mysql_result( $result, $i, "order_value2" );
				$query2 = "SELECT name, village_id, ruler_id " .
						"FROM village " .
						"WHERE x = " . mysql_result( $result, $i, "order_value" ) . " " .
						"AND y = " . mysql_result( $result, $i, "order_value2" );
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				if( isset( $row2[ 'village_id' ] ) ) {
					$village_name = $row2[ 'name' ];
					$village_status = getVillageStatus( $row2[ 'village_id' ] );
					if( $village_status == 3 ) {
						$village_name = translate( "Independent village" );
					}
					$color = getStatusColor( $village_status );
					if( $village_status == 0 ) {
						$description = "<a href=\"../villages/village.php?name=$village_name\"><strong id=\"$color\">$village_name</strong></a> <a href=\"../operations/map.php?x=" . ( mysql_result( $result, $i, "order_value" ) - 20 ) . 
										"&y=" . ( mysql_result( $result, $i, "order_value2" ) - 20 ) . "\" title=\"" . translate( 'To the map' ) . "\">" .
										"<strong id=\"$color\">(" . mysql_result( $result, $i, "order_value" ) . ", " . mysql_result( $result, $i, "order_value2" ) . ")</strong></a>";
					}
					else {
						$description = "<a href=\"../operations/map.php?x=" . ( mysql_result( $result, $i, "order_value" ) - 20 ) . 
										"&y=" . ( mysql_result( $result, $i, "order_value2" ) - 20 ) . "\" title=\"" . translate( 'To the map' ) . "\">" .
										"<strong id=\"$color\">$village_name (" . mysql_result( $result, $i, "order_value" ) . ", " . mysql_result( $result, $i, "order_value2" ) . ")</strong></a>";
					}
				}
				else {
					$description = "<a href=\"../operations/map.php?x=" . ( mysql_result( $result, $i, "order_value" ) - 20 ) . 
									"&y=" . ( mysql_result( $result, $i, "order_value2" ) - 20 ) . "\" title=\"" . translate( 'To the map' ) . "\">" .
									"(" . mysql_result( $result, $i, "order_value" ) . ", " . mysql_result( $result, $i, "order_value2" ) . ")</a>";
				}
			}
			elseif( mysql_result( $result, $i, "order_type" ) == 2 ) {
				if( $calc_turn > 0 ) {
					if( mysql_result( $result, $i, "complete_turn" ) == 0 ) {
						$calc_turn += mysql_result( $result, $i, "order_value" );
					}
					else {
						$calc_turn += mysql_result( $result, $i, "complete_turn" ) - $current_turn;
						//print( "hallo!" );
					}
				}
				$order_type = translate( "Wait" );
				if( mysql_result( $result, $i, "order_value" ) == 1 ) {
					$description = translate( "for 1 day" );
				}
				else {
					$description = translate( "for" ) . " " . mysql_result( $result, $i, "order_value" ) . " " . translate( "days" );
				}
			}
			
			print( "\t\t\t<td>$order_type</td>\r\n"  );
			print( "\t\t\t<td>$description</td>\r\n"  );
			if( $calc_turn > 0 ) {
				$eta = $calc_turn - $current_turn;
				if( $eta == 1 ) {
					$eta = "$eta " . translate( "day" );
				}
				else {
					$eta = "$eta " . translate( "days" );
				}
				print( "<td>$eta</td>" );
			}
			else {
				print( "<td>-</td>" );
			}
		
			if( $follow == -1 && mysql_result( $result, $i, 'no_cancel' ) == 0 /*mysql_result( $result, $i, "complete_turn" ) == 0 || mysql_result( $result, $i, "order_type" ) == 2*/ ) {
				print( "<td><a href=\"delete_queue.php?id=" . mysql_result( $result, $i, "army_order_id" ) . "\"><img src=\"../resources/delete.png\" alt=\"" . translate( "Remove from queue" ) . "\" border=\"0\" /></a></td>" );
			}
			else {
				print( "<td>&nbsp;</td>" );
			}
			print( "</tr>\r\n" );
			$i ++;
		}
		print "</table>\r\n";
	}
	else {
?>
					<div class="blok">
						<?php transprint( 'No orders at the moment.' ); ?>
					</div>
<?php
	}

	if( isset( $_SESSION[ 'follow_order_status' ] ) ) {
?>
					<br />
					<div class="blok">
						<?php transprint( $_SESSION[ 'follow_order_status' ] ); ?>
					</div>
<?php
		unset( $_SESSION[ 'follow_order_status' ] );
	}

	$query = "SELECT count(*) " .
			"FROM army " .
			"WHERE follow = " . $army_id;
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$following_armies = $row[ 'count(*)' ];
	
	if( $ready && $follow == -1 && $size > 0 && $following_armies == 0 ) {
		$query3 = "SELECT faction_id " .
				"FROM faction_member " .
				"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND requested = 0 AND invited = 0";
		$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
		if( isset( $row3[ 'faction_id' ] ) ) {
			$rulers_query = " AND ruler_id IN ( SELECT ruler_id FROM faction_member WHERE requested = 0 AND invited = 0 AND faction_id = " . $row3[ 'faction_id' ] . " )";
		}
		else {
			$rulers_query = " AND ruler_id = " . $_SESSION[ 'ruler_id' ];
		}
		
		$query = "SELECT army_id, name, officer_id, ruler_id " .
				"FROM army " .
				"WHERE army_id != $army_id AND training = 0 AND travel = 0 AND  fortified = 0 AND follow = -1 AND mission = 0 " .
					"AND x = $x AND y = $y AND ( SELECT count(*) FROM army_orders WHERE army_orders.army_id = army.army_id ) = 0 " .
				$rulers_query . " " . 
				"ORDER BY name";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		if( $num > 0 ) {
			$i = 0;
?>
					<br />
					<div class="blok">
						<strong><?php transprint( 'Follow' ); ?></strong><br />
						<?php transprint( 'Make this army follow another army' ); ?>:<br />
						<form method="post" action="submit_follow.php">
							<select name="follow_id" value="-1">
								<option value="-1"><?php transprint( 'None' ) ?></option>
<?php		
			while ( $i < $num ) {
				$select_name = mysql_result( $result, $i, 'name' );
				if( mysql_result( $result, $i, 'officer_id' ) != -1 ) {
					$query2 = "SELECT name " .
							"FROM officers " .
							"WHERE officer_id = " . mysql_result( $result, $i, 'officer_id' );
					$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
					$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
					$select_name .=  ' - ' . $row2[ 'name' ];
				}
				
				$query2 = "SELECT name " .
						"FROM ruler " .
						"WHERE ruler_id = " . mysql_result( $result, $i, 'ruler_id' );
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				$select_name .= ' - ' . $row2[ 'name' ];
				
				print( "<option value=\"" . mysql_result( $result, $i, 'army_id' ) . "\">$select_name</option>" );
				$i ++;
			}
?>							
							</select><br />
							<small>(<?php transprint( 'The army will be given the same orders as the army it is following.' ); ?>)</small><br />
							<input type="hidden" name="army_id" value="<?php print( $army_id ); ?>" />
							<input type="submit" value="<?php transprint( 'Confirm' ); ?>" />
						</form>
					</div>
<?php
		}
	}

	if( isset( $_SESSION[ 'merge_order_status' ] ) ) {
?>
					<br />
					<div class="blok">
						<?php transprint( $_SESSION[ 'merge_order_status' ] ); ?>
					</div>
<?php
		unset( $_SESSION[ 'merge_order_status' ] );
	}

	if( ( $ready_but_following || $ready ) && $size > 0 && $following_armies == 0 ) {
		$query = "SELECT army_id, name, ( scout + infantry + cavalry + artillery ) AS size " .
				"FROM army " .
				"WHERE army_id != $army_id AND training = 0 AND travel = 0 AND fortified = 0 AND mission = 0 " .
					"AND x = $x AND y = $y AND ( SELECT count(*) FROM army_orders WHERE army_orders.army_id = army.army_id ) = 0 " .
					"AND ruler_id = " . $_SESSION[ 'ruler_id' ] . " " .
				"ORDER BY name";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		if( $num > 0 ) {
			$i = 0;
?>
					<br />
					<div class="blok">
						<strong><?php transprint( 'Merge' ); ?></strong><br />
						<?php transprint( 'Move the soldiers from this army into another army at this location' ); ?>:<br />
						<form method="post" action="submit_merge.php">
							<select name="merge_id" value="-1">
								<option value="-1"><?php transprint( 'None' ) ?></option>
<?php
			while ( $i < $num ) {
				$select_name = mysql_result( $result, $i, 'name' );
				$select_name .= ' - ' . number_format( mysql_result( $result, $i, 'size' ) );
				print( "<option value=\"" . mysql_result( $result, $i, 'army_id' ) . "\">$select_name</option>" );
				$i ++;
			}
?>
							</select><br />
							<small>(<?php transprint( 'This army will be removed and it\'s officer will be free to command another army.' ); ?>)</small><br />
							<input type="hidden" name="army_id" value="<?php print( $army_id ); ?>" />
							<input type="submit" value="<?php transprint( 'Confirm' ); ?>" />
						</form>
					</div>
<?php
		}
	}
	
	$query = "SELECT count(*) " .
			"FROM army_orders " .
			"WHERE army_id = " . $army_id;
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	
	if( $row[ 'count(*)' ] < 6 && !$fortified && $follow == -1 ) {
?>
					<br />
					<form method="post" action="que_order.php">
						<input type="hidden" name="army_id" value="<?php print( $army_id ); ?>" />
						<div id="blok_half" class="place-left">
							<strong><?php transprint( 'Give order' ); ?></strong><br />
							<input type="radio" checked="checked" name="order_type" value="1" /> <?php transprint( 'Move' ); ?>
							<div class="sub_block">
								<?php transprint( 'to coordinates' ); ?>:
								<input type="text" size="3" name="x" value="" />
								<input type="text" size="3" name="y" value="" /><br />
								<?php transprint( 'or' ); ?><br />
								<?php transprint( 'to our village' ); ?>: <select onchange="fillHidden( this )" id="target_select">
									<option selected="selected"><?php transprint( 'None' ); ?></option>
<?php
		$query = "SELECT name, x, y " .
				"FROM village " .
				"WHERE ruler_id = '" . $_SESSION[ 'ruler_id' ] . "' " .
				"ORDER BY name";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num= mysql_numrows( $result );
		$i = 0;
		while ( $i < $num ) {
			$target_name =  mysql_result( $result, $i, "name" ) . " (" . mysql_result( $result, $i, "x" ) . ", " . mysql_result( $result, $i, "y" ) . ")";
			print( "<option value=\"" . mysql_result( $result, $i, "name" ) . "\">$target_name</option>" );
			$i ++;
		}
?>
								</select>
								<input type="hidden" id="target_name" name="target_name" value="" /><br />
<?php
		$locations = get_locations();
		$shared_locations = get_shared_locations();
		if( count( $shared_locations ) + count( $locations ) > 0 ) {
?>								
								<?php transprint( 'or' ); ?><br />
								<?php transprint( 'to stored location' ); ?>: 
								<select name="location_id">
									<option value="-1"><?php transprint( 'None' ); ?></option>
<?php
			$printed = false;
			if( count( $locations ) > 0 ) {
				$printed = true;
				foreach( $locations AS $location ) {
?>
									<option value="<?php print( $location->location_id ); ?>"><?php print( get_location_title( $location ) ); ?></option>
<?php				
				}
			}
			if( count( $shared_locations ) > 0 ) {
				if( $printed ) {
?>
									<option value="-1">----------------------------</option>
<?php				
				}
				foreach( $shared_locations AS $location ) {
?>
									<option value="<?php print( $location->location_id ); ?>"><?php print( get_location_title( $location ) ); ?></option>
<?php				
				}
			}
?>
								</select>
<?php
		}
?>								
							</div>
							<br />
							<input type="radio" name="order_type" value="2" /> <?php transprint( 'Wait' ); ?>
							<div class="sub_block">
								<?php transprint( 'Turns to wait' ); ?>: <input type="text" size="3" name="wait_turns" /><br />
							</div><br />
							<input type="submit" value="<?php transprint( 'Give order' ); ?>" />
						</form>
					</div>
<?php 
		$statement = $dal->prepare( 'SELECT COUNT(*) 
			FROM army 
			JOIN officers ON army.officer_id = officers.officer_id 
			WHERE parent_officer_id = :officer_id' );
		$statement->bindParam( ':officer_id', $officer_id, PDO::PARAM_INT );
		if( $dal->getValue( $statement ) > 0 ) {
?>
					<div class="half-block place-right">
						<form method="post" action="advanced_orders.php">
							<input type="hidden" name="army_id" value="<?php print( $army_id ); ?>" />
							<strong><?php transprint( 'Advanced orders' ); ?></strong> (<a href="../operations/edit_locations.php"><?php transprint( 'manage locations' ); ?></a>)<br />
							<br />
<?php
			if( count( $shared_locations ) + count( $locations ) > 0 ) {
?>								
							<?php transprint( 'Gather forces at' ); ?>: 
							<select name="gather_at_location_id">
								<option value="-1"><?php transprint( 'None' ); ?></option>
<?php
			$printed = false;
			if( count( $locations ) > 0 ) {
				$printed = true;
				foreach( $locations AS $location ) {
?>
								<option value="<?php print( $location->location_id ); ?>"><?php print( get_location_title( $location ) ); ?></option>
<?php				
				}
			}
			if( count( $shared_locations ) > 0 ) {
				if( $printed ) {
?>
								<option value="-1">----------------------------</option>
<?php				
				}
				foreach( $shared_locations AS $location ) {
?>
								<option value="<?php print( $location->location_id ); ?>"><?php print( get_location_title( $location ) ); ?></option>
<?php				
				}
			}
?>
							</select><br />
<?php
		}
?>						
						<br />
						<input type="submit" value="<?php transprint( 'Confirm' ); ?>" />
						</form>
					</div>
<?php
		} 
?>					
					<br class="clear" />
<?php
	}
	elseif( $fortified ) {
?>
					<br />
					<div class="blok">
						<?php transprint( 'Armies can\'t move while fortified.' ); ?>
					</div> 
<?php
	}
	elseif( $follow > -1 ) {
		$query = "SELECT name, ruler_id " .
				"FROM army " .
				"WHERE army_id = " . $follow;
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		
		if( $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
			$follow_army = "<a href=\"army.php?name=" . $row[ 'name' ] . "\"><strong class=\"green\">" . $row[ 'name' ] . "</strong></a>";
		}
		else {
			$follow_army = "<strong class=\"blue\">" . $row[ 'name' ] . "</strong>";
		}
?>
					<br />
					<div class="blok">
						<?php transprint( 'This army is following' ); ?> <?php print( $follow_army ); ?><br />
						<br />
						<form method="post" action="submit_follow.php">
							<input type="hidden" name="army_id" value="<?php print( $army_id ); ?>" />
							<input type="hidden" name="follow_id" value="-1" />
							<input type="submit" value="<?php transprint( 'Stop following' ); ?>" />
						</form>
					</div> 
<?php
	}
	else { 
?>
					<br />
					<div class="blok">
						<?php transprint( 'Your army already has the maximum number of orders.' ); ?>
					</div> 
<?php
	}
	$query = "SELECT * " .
			"FROM army " .
			"WHERE follow = $army_id " .
			"ORDER BY name";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	$i = 0;
	if( $num > 0 ) {
?>
					<br />
					<div class="blok">
						<strong><?php transprint( 'This army is followed by' ); ?></strong><br />
						<ul>
<?php
		while ( $i < $num ) {
?>
							<li>
								<a href="army.php?name=<?php print( mysql_result( $result, $i, 'name' ) ); ?>">
									<strong class="<?php ( mysql_result( $result, $i, 'ruler_id' ) == $_SESSION[ 'ruler_id' ] ) ? print( 'green' ) : print( 'blue' ); ?>">
										<?php print( mysql_result( $result, $i, 'name' ) ); ?>
									</strong>
								</a> - <form method="post" action="stop_following.php"><input type="hidden" name="follow_id" value="<?php print( mysql_result( $result, $i, 'army_id' ) ); ?>" /><input type="hidden" name="army_id" value="<?php print( $army_id ); ?>" /><input type="submit" value="<?php transprint( 'Stop following' ); ?>" /></form>
							</li>
<?php
			$i ++;
		}
?>
						</ul>
					</div>
<?php
	}
?>
					<script type="text/javascript">
						function fillHidden() {
							var tmp = document.getElementById( "target_name" );
							var select = document.getElementById( "target_select" );
							tmp.setAttribute( "value", select.value );
						}
					</script>
					<?php $xajax->printJavascript('../global/'); ?>
					<script language="JavaScript" type="text/javascript">
					  function getTravelTime() {
					  	var traveltime = document.getElementById( "traveltime" );
					  	if( traveltime ) {
					  		traveltime.innerHTML = "Loading...";
					  	}
					  	xajax_travelTimeAJAX( document.getElementById( 'army_id' ).value, document.getElementById( 'x_target' ).value, 
					  		document.getElementById( 'y_target' ).value, document.getElementById( 'x' ).value, document.getElementById( 'y' ).value );
					  }
					</script>
<?php
	aos_footer();
}
else {
?>
					<div class="blok">
						<?php transprint( 'You don\'t own this army!' ); ?>
					</div>
<?php
	aos_footer();
}
?>